package com.ffspace.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;






import com.ff.blog.common.BaseResult;
import com.ff.blog.constant.Constants;
import com.ff.blog.enums.ActionType;
import com.ff.blog.util.BeanUtil;
import com.ffspace.blog.core.dal.dataobject.Blog;
import com.ffspace.blog.core.dal.dataobject.User;
import com.ffspace.blog.service.BlogService;
import com.ffspace.blog.service.UserService;

@Controller
public class UserController {
	
	private static Logger	logger = LoggerFactory.getLogger(UserController.class);
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private BlogService blogService;	
	
	@ResponseBody
	@RequestMapping("/login.htm")
	public BaseResult<User> login(HttpServletRequest request) throws Exception
	{
		
		String loginName = request.getParameter("loginName");
		BlogService blogService = (BlogService)BeanUtil.getBean("blogService");
		String password = request.getParameter("password");
		BaseResult<User> result = userService.login(loginName, password);
		
		if (result.isSuccess())
		{
			request.getSession().setAttribute(Constants.USER_KEY, result.getData());
		}
		
		return result;
	}

	@ResponseBody
	@RequestMapping("/register.htm")
	public BaseResult<User> register(HttpServletRequest request) throws Exception
	{
		BaseResult<User> result = new BaseResult<User>();
		try
		{
			String loginName = request.getParameter("loginName");
			String password = request.getParameter("password");
			String nickName = request.getParameter("nickName");
			String email = request.getParameter("email");
	
			User user = new User();
			user.setEmail(email);
			user.setLoginName(loginName);
			user.setNickName(nickName);
			user.setPassword(password);
			userService.register(user);
			
			result.setSuccess(true);
			result.setMessage("注册成功");
		}
		catch (Exception e)
		{
			logger.error("active error", e);
			result.setSuccess(false);
			result.setMessage("服务器错误");
		}
		return result;
	}
	

	@RequestMapping("/active.htm")
	public String active(HttpServletRequest request, ModelMap model) throws Exception
	{
		model.addAttribute("type", ActionType.ACTIVE.getCode());
		String token = request.getParameter("token");

		BaseResult<String>	result = userService.active(token);
		if (result.isSuccess())
		{
			return "success";
		}

		return "error";
	}
	
	@RequestMapping("/forwardSuccess.htm")
	public String forwardSuccess(HttpServletRequest request, String type, ModelMap model) throws Exception
	{
		model.addAttribute("type", type);

		return "success";

	}
	
	@RequestMapping("/index.htm")
	public String forwardIndex(HttpServletRequest request, Integer pageNum, Integer currentPage, ModelMap model) throws Exception
	{
		try
		{
			if (pageNum == null)
			{
				pageNum = 10;
			}
			
			if (currentPage == null)
			{
				currentPage = 1;
			}
			
			Map<String, Object> condition = new HashMap<String, Object>();
			int offset = (currentPage - 1) * pageNum;
			if (offset < 0)
			{
				offset = 0;
			}
			condition.put("limit", pageNum);
			condition.put("offset", offset);
			BaseResult<List<Blog>> result = blogService.queryPage(condition);
			if (result.isSuccess())
			{
				model.addAttribute("blogList", result.getData());
			}
			
		}
		catch (Exception e)
		{
			logger.error("forwardIndex error", e);
			return "error";
		}

		return "index";

	}
	
}
